//
//  BFSettingViewController.m
//  13KModel
//
//  Created by jack on 5/09/12.
//  Copyright (c) 2012 __butterfly__. All rights reserved.
//
// CLASS INCLUDES
#import "SDImageCache.h"
#import "BFSettingViewController.h"

// CONST DEFINE
NSInteger const kAcivityIndicatorViewTag = 100;
NSInteger const kCacheSizeInfoViewTag    = 101;

@interface BFSettingViewController (){
    UITableViewCell *clearCacheCell;
}

- (void)initNavBar;
- (void)initSettingArray;
- (void)basicSettingClick:(NSInteger)index;
- (void)reloadCacheSize;
@end

@implementation BFSettingViewController
@synthesize settingTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initSettingArray];
    
    [self initNavBar];
}

- (void)initNavBar
{
    // 返回按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal];
    [button addTarget:self
               action:@selector(navigateToPrevView) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0,
                                button.imageView.image.size.width,
                                button.imageView.image.size.height)];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = backButton;
    self.title = @"设置";
}

- (void)initSettingArray
{
    NSArray *basicSetting = [NSArray arrayWithObjects:NSLocalizedString(@"清除缓存", nil),
                                                      NSLocalizedString(@"亲, 给个评价吧!", nil),
                                                      NSLocalizedString(@"更多精彩", nil),
                                                      NSLocalizedString(@"关于", nil), nil];
    
    NSArray *loginSetting = [NSArray arrayWithObjects:NSLocalizedString(@"注销", nil),
                                                      NSLocalizedString(@"登录新浪微博", nil), nil];
    
    settingAttrArray = [NSArray arrayWithObjects:basicSetting, loginSetting, nil];
}

#pragma mark- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section > 0) {
        return 1;
    }
    else{
        return [(NSArray*)[settingAttrArray objectAtIndex:0] count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"tableCellIdentifier";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
	if (cell == nil) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:cellIdentifier];
	}
    if (indexPath.section > 0) {
        cell.accessoryView = nil;
        cell.accessoryType  = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.textAlignment = UITextAlignmentCenter;
        
        if ([wbEngine isLoggedIn] && ![wbEngine isAuthorizeExpired]) {
            cell.textLabel.text = [(NSArray*)[settingAttrArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        }
        else{
            cell.textLabel.text = [(NSArray*)[settingAttrArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row+1];
        }
    }
    else{
        cell.accessoryView  = nil;
        cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.text = [(NSArray*)[settingAttrArray objectAtIndex:indexPath.section]
                               objectAtIndex:indexPath.row];
    }
    
	return cell;
}

#pragma mark- UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
	
    if (indexPath.section == 0) {
        [self basicSettingClick:indexPath.row];
    }
    else{
        if ([wbEngine isLoggedIn] && ![wbEngine isAuthorizeExpired]) {
            [wbEngine logOut];
        }
        else{
            [wbEngine logIn];
        }
    }
}

- (void)basicSettingClick:(NSInteger)index
{
    switch (index) {
        case 0:{
                [[SDImageCache sharedImageCache] cleanDisk];
                [self showAlertMessageBox:NSLocalizedString(@"清除完成!", nil) delay:1.0f];
        }
            break;
        case 1:{
//            NSString *str = [NSString stringWithFormat:
//                             @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa     /wa/viewContentsUserReviews?type=Purple+Software&id=%d",
//                             m_appleID];
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
            break;
        case 2:{
            UIViewController *hotViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Hot"];
            [self.navigationController pushViewController:hotViewController animated:YES];
        }
            break;
        case 3:{
            UIViewController *aboutViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"About"];
            [self.navigationController pushViewController:aboutViewController animated:YES];
        }
            break;
        default:
            break;
    }
}

#pragma mark - WBEngineDelegate
- (void)engineDidLogIn:(WBEngine *)engine
{
    [self.settingTableView reloadData];
}

- (void)engineDidLogOut:(WBEngine *)engine
{
    [self showAlertMessageBox:NSLocalizedStringFromTable(@"账号注销成功", @"message", nil)
                        delay:2.0f];
    [self.settingTableView reloadData];
}

- (void)engine:(WBEngine *)engine didFailToLogInWithError:(NSError *)error
{
}

#pragma mark - release

- (void)viewDidUnload {
    [self setSettingTableView:nil];
    [super viewDidUnload];
}
@end
